#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 5e3 + 10;

int n, m;
LL a[N][N], f[N][N];

int main()
{
	cin >> n >> m;
	for(int i = 1;i <= n;i ++)
	{
		int x, y, v; cin >> x >> y >> v;
		a[++ x][++ y] += v;
	}

	for(int i = 1;i < N;i ++)
		for(int j = 1;j < N;j ++)
			f[i][j] = f[i][j - 1] + f[i - 1][j] - f[i - 1][j - 1] + a[i][j];

	LL ans = 0;
	for(int x1 = 1;x1 < N;x1 ++)
	{
		for(int y1 = 1;y1 < N;y1 ++)
		{
			int x2 = min(N - 1, x1 + m - 1), y2 = min(N - 1, y1 + m - 1);
			ans = max(ans, f[x2][y2] - f[x2][y1 - 1] - f[x1 - 1][y2] + f[x1 - 1][y1 - 1]);
		}
	}
	cout << ans << endl;
	return 0;
}